package com.cxhc.app.api;


import com.cxhc.app.api.vo.TreeVo;
import com.cxhc.common.annotation.Anonymous;
import com.cxhc.common.core.domain.R;
import com.cxhc.common.core.domain.entity.SysUser;
import com.cxhc.common.utils.SecurityUtils;
import com.cxhc.system.domain.app.HomeApp;
import com.cxhc.system.domain.app.Tree;
import com.cxhc.system.domain.app.TreeInspect;
import com.cxhc.system.domain.app.TreeType;
import com.cxhc.system.service.app.IHomeAppService;
import com.cxhc.system.service.app.ITreeInspectService;
import com.cxhc.system.service.app.ITreeService;
import com.cxhc.system.service.app.ITreeTypeService;
import com.google.common.collect.ImmutableMap;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 绿化模块接口
 */
@RestController()
@RequestMapping("/api/tree")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiTree {

    @Resource
    private ITreeService treeService;
    @Resource
    private ITreeTypeService treeTypeService;
    @Resource
    private ITreeInspectService inspectService;

    /**
     * 树木类型列表
     *
     * @return
     */
    @Anonymous
    @GetMapping("/typeList")
    private R<List<TreeType>> typeList() {
        return R.ok(treeTypeService.selectTreeTypeList(new TreeType()));
    }

    @Anonymous
    @GetMapping("/list")
    private R<List<Tree>> list(TreeVo vo) {
        return R.ok(treeService.selectTreeList(new Tree().setName(vo.getName()).setType(vo.getTypeId())));
    }

    /**
     * 树木详情
     *
     * @param id
     * @return
     */
    @Anonymous
    @GetMapping("/info/{id}")
    private R<Map<String, Object>> info(@PathVariable Long id) {
        return R.ok(ImmutableMap.of("tree", treeService.selectTreeById(id),
                "inspectList", inspectService.selectTreeInspectList(new TreeInspect().setTreeId(id))));
    }

    /**
     * 新增巡检
     *
     * @param inspect
     * @return
     */
    @PostMapping("/postInspect")
    private R<String> postInspect(@RequestBody @Valid TreeInspect inspect) {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        inspect.setUserId(SecurityUtils.getUserId());
        inspect.setUserName(loginUser.getNickName());
        inspect.setDeptName(loginUser.getDept().getDeptName());
        inspectService.insertTreeInspect(inspect);
        return R.ok();
    }

}
